<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\WithMapping;
use Illuminate\Contracts\Queue\ShouldQueue;

abstract class BasicImport implements WithBatchInserts, WithStartRow, WithMapping, WithCalculatedFormulas, WithChunkReading
{
    protected $header;
    protected $chunk_size = 1000;

    //批量导入1000条
    public function batchSize(): int
    {
        return 1000;
    }

    //以1000条数据基准切割数据
    public function chunkSize(): int
    {
        return $this->chunk_size;
    }

    public function startRow(): int
    {
        return 2;
    }

    public function map($row): array
    {
        $mapped = [];
        foreach ($this->header as $key => $value) {
            if ($value) {
                $val = preg_replace("/^[\s\v".chr(194).chr(160)."]+/","", $row[$key]);
                $mapped[$value] = preg_replace("/[\s\v".chr(194).chr(160)."]+$/","", $val);
            }
//            if (isset($row[$key])){
//                if ($value) {
//                    $val = preg_replace("/^[\s\v".chr(194).chr(160)."]+/","", $row[$key]);
//                    $mapped[$value] = preg_replace("/[\s\v".chr(194).chr(160)."]+$/","", $val);
//                }else{
//                    $mapped[$value] = '';
//                }
//            }
        }
        return $mapped;
    }

}
